package com.fingard.dsp.bank.directbank.icbc02;

import java.util.Date;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.directbank.bankInterface.ICurrencyExchange;
import com.fingard.dsp.bank.reqrespobj.currencyexchange.ReqExchange;
import com.fingard.dsp.bank.reqrespobj.currencyexchange.ReqQryExchange;
import com.fingard.dsp.bank.reqrespobj.currencyexchange.RetAtsExchange;
import com.fingard.dsp.bank.reqrespobj.currencyexchange.detail.RetAtsExchangeDetail;
import com.fingard.net.ConnState;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

public class ICBC02PurchaseExchange extends ICBC02Base implements ICurrencyExchange {
    //购汇 0.0.1.0
    @Override
    public RetAtsExchange exchangePay(ReqExchange exchangePay) throws Exception {
        RetAtsExchange retAts = new RetAtsExchange(exchangePay.reqHeader);
        RetAtsExchangeDetail retDetail = new RetAtsExchangeDetail();
        bnkPackageID = exchangePay.singleDetail.askNumber;
        bnkfSeqno = exchangePay.singleDetail.askNumber;
        bnkVersion = "0.0.1.0";
        bnkTransCode = "BUYEXCHGE";
        XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
        //写入ICBC02通用的头部节点
        writeCommonHeader(xmlWriter);
        xmlWriter.writeElementString("PayAcct", exchangePay.singleDetail.payAct);//人民币账户
        xmlWriter.writeElementString("PayAmt", exchangePay.singleDetail.payAmount);//转入外币金额
        xmlWriter.writeElementString("RecAcct", exchangePay.singleDetail.recAct);//外币账户
        xmlWriter.writeElementString("Country", exchangePay.singleDetail.recCountryCode);//Z00： 一般贸易区Z01： 保税区Z02： 加工区Z03： 钻石交易所
        xmlWriter.writeElementString("ExchType", exchangePay.singleDetail.exchangeType);//need 购汇类型代码 todo 提供查询接口？ATS提供初始化数据
        xmlWriter.writeElementString("AgentType", "");//被代理客户类型 1、居民个人；2、非居民个人；3、公司客户
        xmlWriter.writeElementString("SignTime", Format.DateTime17Format.format(new Date()));
        xmlWriter.writeEndDocument();//写入报文完成
        String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), true);//发送银行
        if (tmpStrRet[0].length() == 0) {
            StringBuilder tmpSbMsg = new StringBuilder();
            //解析银行返回
            XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
            if (tmpBkReader != null) {
                String tmpRetCode = tmpBkReader.getFirstText("/CMS/eb/pub/RetCode");
                String tmpRetMsg = tmpBkReader.getFirstText("/CMS/eb/pub/RetMsg");
                if (!StringHelper.isNullOrEmpty(tmpRetCode)) {
                    retDetail.respCode = "0";
                    retDetail.respInfo = tmpRetMsg;
                    retDetail.reqSeqID = tmpBkReader.getFirstText("/CMS/eb/pub/fSeqno");
                    if (tmpBkReader.read("/CMS/eb/out")) {
                        retDetail.transState = tmpRetCode + "+" + tmpBkReader.readedXElement.getSingleText("Result");
                        retDetail.payInfoCode = retDetail.transState;
                        retDetail.respInfo = tmpRetMsg;
                    } else {
                        retDetail.respCode = "0";
                        retDetail.transState = tmpRetCode + "+";
                        retDetail.payInfoCode = tmpRetCode;
                        retDetail.respInfo = tmpRetMsg;
                        retDetail.reqSeqID = exchangePay.singleDetail.askNumber;
                    }
                } else {
                    retDetail.respCode = "-1";
                    retDetail.respInfo = "/CMS/eb/pub/RetCode节点为空";
                }
            } else {
                retDetail.respCode = "-1";
                retDetail.respInfo = tmpSbMsg.toString();
            }
        } else if (tmpStrRet[0].equals(ConnState.FAIL)) {
            retDetail.respCode = "0";
            retDetail.respInfo = tmpStrRet[1];
            retDetail.reqSeqID = exchangePay.singleDetail.askNumber;
            retDetail.transState = PaymentState.FAIL_Payment;
            retDetail.payInfoCode = tmpStrRet[0];
        } else {
            retDetail.respCode = "-1";
            retDetail.respInfo = tmpStrRet[1];
        }
        retAts.writeRetDetail(retDetail);
        return retAts;
    }
    
    //购汇状态查询
    @Override
    public RetAtsExchange qryExchangePay(ReqQryExchange exchangeQry) throws Exception {
        //返回ATS的对象

        RetAtsExchange retAts = new RetAtsExchange(exchangeQry.reqHeader);
        RetAtsExchangeDetail retDetail = new RetAtsExchangeDetail();
        bnkPackageID = exchangeQry.reqHeader.transSeqID;
        bnkTransCode = "QBUYEXCHGE";
        bnkfSeqno = bnkPackageID;
        XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
        writeCommonHeader(xmlWriter);//写入ICBC02通用的头部节点
        /*
      <QryfSeqno></QryfSeqno>
      <QrySerialNo></QrySerialNo>
      <ReqReserved1></ReqReserved1>
      <ReqReserved2></ReqReserved2>
	  */
        xmlWriter.writeElementString("QryfSeqno", exchangeQry.askNumber);
        xmlWriter.writeElementClosed("QrySerialNo");
        xmlWriter.writeEndDocument();

        try {
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();

                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);

                if (tmpBkReader != null) {
                    String tmpRetCode = tmpBkReader.getFirstText("/CMS/eb/pub/RetCode");
                    String tmpRetMsg = tmpBkReader.getFirstText("/CMS/eb/pub/RetMsg");
                    if (!StringHelper.isNullOrEmpty(tmpRetCode)) {
                        retDetail.reqSeqID = tmpBkReader.getFirstText("/CMS/eb/out/QryfSeqno");
                        if (tmpBkReader.read("/CMS/eb/out/rd")) {
                            retDetail.respCode = "0";
                            String iRetMsg = tmpBkReader.readedXElement.getSingleText("iRetMsg");
                            if (!StringHelper.isNullOrEmpty(iRetMsg)) {
                                retDetail.respInfo = iRetMsg;
                            } else {
                                retDetail.respInfo = tmpRetMsg;
                            }
                            String iRetCode = tmpBkReader.readedXElement.getSingleText("iRetCode");
                            if (!StringHelper.isNullOrEmpty(iRetCode)) {
                                retDetail.payInfoCode = iRetCode;
                            } else {
                                retDetail.payInfoCode = tmpRetCode;
                            }

                            String tmpResult = "";
                            if (tmpBkReader.readedXElement.getSingleText("Result") != null) {
                                tmpResult = tmpBkReader.readedXElement.getSingleText("Result");
                            }
                            retDetail.transState = tmpRetCode + "+" + tmpResult;//返回银行状态码
                        } else {
                            retDetail.respCode = "-1";
                            retDetail.respInfo = tmpRetMsg;
                        }
                    } else {
                        retDetail.respCode = "-1";
                        retDetail.respInfo = "节点/CMS/eb/pub/RetCode为空";
                    }
                } else {
                    retDetail.respCode = "-1";
                    retDetail.respInfo = tmpSbMsg.toString();
                }
            } else {
                retDetail.respCode = "-1";
                retDetail.respInfo = tmpStrRet[1];
            }
        } catch (Exception e) {
            retDetail.respCode = "-1";
            retDetail.respInfo = e.getMessage();
            e.printStackTrace();
        }
        retAts.writeRetDetail(retDetail);
        return retAts;
    }
    
}
